package hung.vnptthanhhoa.cookingbook;

import hung.vnptthanhhoa.util.JSONParser;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

public class Fragment_Food_Detail extends Fragment {

	private String url = "http://10.41.23.115/getFoodById.php";
	private byte[] image;

	public Fragment_Food_Detail(int food_id, byte[] image) {
		url = url + "?id=" + food_id;
		this.image = image;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_food_detail,
				container, false);

		new JSON_Food_Detail(rootView).execute();
		return rootView;
	}

	private class JSON_Food_Detail extends
			AsyncTask<String, String, JSONObject> {

		private View view;
		private ProgressDialog pDialog;

		public JSON_Food_Detail(View view) {
			this.view = view;
		}

		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(getActivity());
			pDialog.setMessage("Đang tải dữ liệu...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected JSONObject doInBackground(String... params) {

			JSONParser jParser = new JSONParser();
			JSONObject json = jParser.getJSONFromUrl(url);
			return json;

		}

		protected void onPostExecute(JSONObject json) {
			pDialog.dismiss();
			try {
				JSONArray jArray = json.getJSONArray("post");
				JSONObject e = jArray.getJSONObject(0);
				WebView web_making = (WebView) view
						.findViewById(R.id.web_making);
				WebView web_material = (WebView) view
						.findViewById(R.id.web_material);
				TextView textView_Title = (TextView) view
						.findViewById(R.id.title_food);
				TextView textView_summary = (TextView) view
						.findViewById(R.id.summary_food);
				TextView textView_tham_khao = (TextView) view
						.findViewById(R.id.tham_khao_food);
				ImageView imageFood = (ImageView) view
						.findViewById(R.id.image_food);
				final String mineType = "text/html";
				final String encoding = "UTF-8";
				textView_Title.setText(e.getString("name"));
				textView_summary.setText(e.getString("note"));
				imageFood.setImageBitmap(BitmapFactory.decodeByteArray(image,
						0, image.length));
				web_material.getSettings().setLayoutAlgorithm(
						LayoutAlgorithm.SINGLE_COLUMN);
				web_material.loadDataWithBaseURL("", e.getString("material"),
						mineType, encoding, "");
				web_making.getSettings().setLayoutAlgorithm(
						LayoutAlgorithm.SINGLE_COLUMN);
				web_making.loadDataWithBaseURL("", e.getString("making"),
						mineType, encoding, "");
				if (e.getString("tham_khao").length() > 0)
					textView_tham_khao.setText("Nguồn tham khảo: "
							+ e.getString("tham_khao"));

			} catch (Exception e) {

			}
		}

	}

}
